package gs.gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

public class NPCLocationPanel  extends JPanel {
	AdminWindow parent;
	private ConnectionController connection;
	
	JComboBox nameField;
	JComboBox locationField;
	JFormattedTextField xField;
	JFormattedTextField yField;
	JButton addButton;
	JButton deleteButton;
	DefaultListModel npcModel;
	JList npcList;
	JScrollPane npcScrollPane;
	
	NPCLocationPanel(ConnectionController c, AdminWindow win)
	{
		super();
		connection = c;
		parent = win;
	
		this.setLayout(new GridLayout(1,2));
		
		JPanel addPane = new JPanel(new GridLayout(9,2));
		JPanel delPane = new JPanel(new BorderLayout());
		
		addPane.setBorder(
                BorderFactory.createCompoundBorder(
                                BorderFactory.createTitledBorder("Dodaj"),
                                BorderFactory.createEmptyBorder(5,5,5,5)));
		
		delPane.setBorder(
                BorderFactory.createCompoundBorder(
                                BorderFactory.createTitledBorder("Lista"),
                                BorderFactory.createEmptyBorder(5,5,5,5)));
		
		nameField = new JComboBox<String>();
		locationField = new JComboBox<String>();
		
		NumberFormat nf = NumberFormat.getNumberInstance();
		nf.setParseIntegerOnly(true);
		xField = new JFormattedTextField(nf);
		xField.setValue(new Integer(0));
		yField = new JFormattedTextField(nf);
		yField.setValue(new Integer(0));
		
		addButton = new JButton("Dodaj");
		addButton.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent event) {
		        addNPCLocation();
		    }
		});
		/*
		deleteButton = new JButton("Usu�");
		deleteButton.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent event) {
		        deleteNPCLocation();
		    }
		});
		*/
		npcModel = new DefaultListModel();
		
		npcList = new JList(npcModel);
		npcList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        npcList.setSelectedIndex(0);
        npcList.setVisibleRowCount(5);
        npcScrollPane = new JScrollPane(npcList);
        
        getNPCLocationList();
        getLocationList();
        
        addPane.add(new JLabel("NPC:"));
        addPane.add(nameField);
        addPane.add(new JLabel("Lokacja:"));
        addPane.add(locationField);
        addPane.add(new JLabel("x:"));
        addPane.add(yField);
        addPane.add(new JLabel("y:"));
        addPane.add(xField);
        
        
        addPane.add(new JLabel(""));
        addPane.add(addButton);
        
        delPane.add(npcScrollPane, BorderLayout.CENTER);
        //delPane.add(deleteButton, BorderLayout.SOUTH );
        
        add(addPane);
        add(delPane);
        
	
	}

	private void getLocationList() {
		ArrayList<String> a;
		a = connection.getLocationList();
		
		nameField.removeAllItems();
		locationField.removeAllItems();
		nameField.addItem("");		
		locationField.addItem("");

		for(String s : a)
		{
			locationField.addItem(s);
		}
		
		a = connection.getNPCList();
		
		for(String s : a)
		{
			nameField.addItem(s);
		}
	}

	private void getNPCLocationList() {
		ArrayList<String> a;
		a = connection.getNPCLocationList();
		npcModel.clear();
		for(String s : a)
		{
			npcModel.addElement(s);
		}
	}
	
	private void addNPCLocation() {
		String x = String.valueOf(yField.getValue());
		String y = String.valueOf(xField.getValue());
		String npcName = (String) nameField.getSelectedItem();
		String locationName = (String) locationField.getSelectedItem();
		
		String err = connection.createNPCLocation(npcName, locationName, x, y);
		if(err == null)
		{
			parent.showSuccess("Dodano NPC "+npcName+" do lokacji "+locationName);
			getNPCLocationList();
		}
		else
		{
			parent.showError("B��d: "+err);
		}
	}

		public void refresh()
		{
			getLocationList();
			getNPCLocationList();		
		}
}
